<?php
/**
 * Object representing a tag
 *
 * @package Concerto
 * @subpackage Models
 * @author Jason Raede <jason@torchdm.com>
 */
namespace application\models;
use application\utilities\Utility;
class Tag extends DBObject{
	/**
	 * tag_id
	 *
	 * @param int
	 */
	protected $id;
	
	/**
	 * tag_title
	 *
	 * @param string
	 */
	protected $title;
	
	/**
	 * tag_permalink
	 *
	 * @param string
	 */
	protected $permalink;
	
	/**
	 * tag_group
	 *
	 * @param int
	 */
	protected $group;
	
	/**
	 * tag_content_type
	 *
	 * @param int
	 */
	protected $contentType;
	
	/**
	 * tag_order
	 *
	 * @param int
	 */
	protected $order;
	
	/**
	 * tag_type
	 *
	 * @param string
	 */
	protected $type;
	
	/**
	 * If $id is provided, initialize object with its database data
	 *
	 * @param int $id
	 */
	public function __construct($id = null) {
		if($id) {
			$row = DB()->getRow("SELECT * FROM `cn_tags` WHERE `tag_id`='$id'");
			$this->id = $id;
			$this->title = $row->tag_title;
			$this->permalink = $row->tag_permalink;
			$this->group = $row->tag_group;
			$this->contentType = $row->tag_content_type;
			$this->type = $row->tag_type;
			$this->order = $row->tag_order;
			
			
		}
	}
	
	/** 
	 * Initialize the object with data from $params
	 *
	 * @param array $params
	 */
	public function create($params) {
		if($params['title'] && $params['contentType'] && $params['type']) {
			$this->title = $params['title'];
			$this->contentType = $params['contentType'];
			$this->type = $params['type'];
			$this->group = $params['group'];
			$this->order = $params['order'];
			$this->id = $params['id'];
			
			// Generate the permalink
			if(!$params['permalink']) 
				$this->permalink = Utility::makeUniqueUrl($params['title']);
			else $this->permalink = Utility::makeUniqueUrl($params['permalink']);
		}
		else {
			Utility::triggerError('You must provide a title, content type, and tag type to create a new tag.');
		}
	}
	
	/** 
	 * @deprecated Merged into save()
	 */
	public function add() {
		return $this->save();
		/**global $concerto;
		$return = array();
		// Make sure there's no group or tag with the same name
		$sameName = DB()->getCount("SELECT `tag_id` FROM `cn_tags` WHERE `tag_content_type`='$this->contentType' AND `tag_title` LIKE '$this->title' AND `tag_type`='$this->type'");
		if($sameName) {
			$return['errors'][] = 'You must give your tag or tag group a unique name.';
		}
		
		if($this->order && $this->type == 'tag' && !is_numeric($this->order)) {
			$return['errors'][] = 'Tag order must be numeric.';
		}
		if(!$this->group && $this->type == 'tag') {
			$return['errors'][] = 'Please select a tag group.';
		}
		
		if(!count($return['errors'])) {
			$concerto->DB->insert('cn_tags', array('tag_title'=>$this->title, 'tag_permalink'=>$this->permalink, 'tag_group'=>$this->group, 'tag_type'=>$this->type, 'tag_content_type'=>$this->contentType, 'tag_order'=>$this->order));
			$return['successes'][] = 'Tag successfully added.';
		}
		return $return;*/
		
	}
	
	/** 
	 * Insert or update the database row
	 *
	 * @return array DBObject status array.
	 */
	public function save() {
		global $concerto;
		$errors = array();
		// Make sure there's no group or tag with the same name
		$sameName = DB()->getCount("SELECT `tag_id` FROM `cn_tags` WHERE `tag_content_type`='$this->contentType' AND `tag_title` LIKE '$this->title' AND `tag_type`='$this->type' AND `tag_id` != '$this->id'");
		if($sameName) {
			$errors[] = 'You must give your tag or tag group a unique name.';
		}
		if($this->order && $this->type == 'tag' && !is_numeric($this->order)) {
			$errors[] = 'Tag order must be numeric.';
		}
		if(!$this->group && $this->type == 'tag') {
			$errors[] = 'Please select a tag group.';
		}
		if(count($errors)) {
			return array('status'=>'error', 'errors'=>$errors);
		}
		else {
			$dbFields = array('tag_title'=>$this->title, 'tag_permalink'=>$this->permalink, 'tag_group'=>$this->group, 'tag_type'=>$this->type, 'tag_content_type'=>$this->contentType, 'tag_order'=>$this->order, 'tag_id'=>$this->id);
			if($this->id) {
				
				$concerto->DB->update('cn_tags', 'tag_id', $this->id, $dbFields);
				$return['successes'][] = 'Tag/tag group updated successfully.';
			}
			else {
				DB()->insert('cn_tags', $dbFields);
				$this->id = DB()->lastInsert();
				 
			}
			return array('status'=>'success', 'id'=>$this->id);
		}
	}
	
	/**
	 * Delete the tag from the database
	 *
	 * @return array DBObject status array.
	 */
	public function delete() {
		global $concerto;
		$concerto->DB->query("DELETE FROM `cn_tags` WHERE `tag_id`='$this->id'");
		$concerto->DB->query("DELETE FROM `cn_content_tag_relations` WHERE `ctr_tag`='$this->id'");
		$concerto->DB->query("DELETE FROM `cn_tags` WHERE `tag_group`='$this->id'");
		return array('status'=>'success');
	}
	

}
?>